/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author tho.hoang
 */
public abstract class AGridItem extends AItem {

    protected Image image;
    protected String text;
    protected String[] textRows = null;
    protected Style style;
    private int maxWidth, maxHeight;

    public AGridItem(Image image, String text) {
        this.image = image;
        this.text = text;
    }

    public AGridItem(String id, int x, int y, int width, int height, boolean focus, boolean visible, Image image, String text) {
        super(id, x, y, width, height, focus, visible);
        this.image = image;
        this.text = text;
    }

    public void paint(int x, int y, Graphics g) {
        paint(x, y, g, false);
    }

    protected void setTextRows(String text) {
        if (text.length() > 0 && style != null) {
            int w = 0;
            style.setWidth(width);
            if (image != null) {
                w = width - image.getWidth() - style.getPadding().getLeft() - style.getPadding().getLeftAndRight();
            } else {
                w = width - style.getPadding().getLeftAndRight();
            }
//            textRows = boldFont.splitWith(text, w);
            textRows = new String[]{text};
        } else {
            textRows = new String[]{""};
        }
    }

    public abstract void paint(int x, int y, Graphics g, boolean validate);

    public void setParent(ACanvas parent) {
        super.setParent(parent);
        if (parent != null) {
            this.style = parent.getStyle().getGridMenuStyle();
        }
    }

    public boolean isPaint(int x, int y) {
        if (!visible) {
            return true;
        }
        int itemX = parent.getItemX(), itemY = parent.getItemY(), parentW = parent.getWidth(), parentH = parent.getHeight() - parent.getFooterHeight();
        if (x < itemX || x > parentW) {
            return false;
        }

        if (y + height < itemY || y > parentH) {
            return false;
        }

        return true;
    }

    public boolean isSelectedPaint(int x, int y) {
        if (!visible) {
            return true;
        }
        int itemX = parent.getItemX(), itemY = parent.getItemY(), parentW = parent.getWidth(), parentH = parent.getHeight() - parent.getFooterHeight();
        if (x < itemX || x + getWidth() > parentW) {
            return false;
        }

        if (y < itemY || y + getHeight() > parentH) {
            return false;
        }

        return true;
    }

    public Image getImage() {
        return image;
    }

    public void setImage(Image image) {
        this.image = image;
    }

    public Style getStyle() {
        return style;
    }

    public void setStyle(Style style) {
        this.style = style;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String[] getTextRows() {
        return textRows;
    }

    public void setTextRows(String[] textRows) {
        this.textRows = textRows;
    }

    public int getMaxWidth() {
        return maxWidth;
    }

    public void setMaxWidth(int maxWidth) {
        this.maxWidth = maxWidth;
    }

    public int getMaxHeight() {
        return maxHeight;
    }

    public void setMaxHeight(int maxHeight) {
        this.maxHeight = maxHeight;
    }
}
